<template>
  <div>
    <div>
      <!-------------------------------表格 ----------------------down ------------------->
      <el-card v-show="!editVisible" :body-style="{padding:'0px 8px'}">
        <!-------------------------------按钮 ----------------------down------------------->
        <div>
          <el-button class="hand-btn" type="primary" icon="el-icon-edit" @click="add">新增</el-button>
          <el-input v-model="query.name" class="handle-input" placeholder="指标名称" />
          <el-button class="hand-btn" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        </div>
        <!-------------------------------按钮 ----------------------up ------------------->
        <el-table
          ref="multipleTable"
          v-loading="loadingMTable"
          :data="page.content"
          border
          max-height="580"
          height="580"
          :cell-style="{'padding':'2px','font-size':'12px'}"
          :header-cell-style="{'padding':'0px','font-weight':'800'}"
          class="table"
          header-cell-class-name="table-header"
        >
          <el-table-column
            type="index"
            width="50"
          />
          <el-table-column prop="id" label="ID" sortable width="80" />
          <el-table-column prop="basePlan.name" label="项目名称" sortable width="200px" />
          <el-table-column prop="name" label="指标名称" sortable width="200px" />
          <el-table-column prop="describe" label="指标描述" sortable />
          <el-table-column label="操作" width="120" align="center">
            <template slot-scope="scope">
              <el-button
                type="text"
                icon="el-icon-edit"
                @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button>
              <el-button
                type="text"
                icon="el-icon-delete"
                class="red"
                @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-card :body-style="{padding:'5px 20px'}">
          <el-pagination
            background
            layout="total,sizes, prev, pager, next"
            :page-sizes="[10,20, 30, 40, 50]"
            :current-page="query.pageIndex"
            :page-size="query.pageSize"
            :total="page.totalElements"
            @size-change="handleSizeChange"
            @current-change="handlePageChange"
          />
        </el-card>
      </el-card>
      <!-------------------------------表格 ----------------------up ------------------->
      <!----------------------------------form 表单------------------down----------- -->
      <el-card v-show="editVisible" :body-style="{padding:'0px 8px'}" style="background-color: #f3f3f3">
        <!-------------------------------按钮 ----------------------down------------------->
        <div>
          <el-button class="hand-btn" type="info" icon="el-icon-back" @click="editVisible = !editVisible">返回</el-button>
          <el-button class="hand-btn" type="primary" icon="el-icon-edit" @click="add">新增</el-button>
          <el-button type="warning" icon="el-icon-plus" @click="open = true">选择所属项目</el-button>
          <el-button class="hand-btn" type="success" @click="saveEdit" :loading="loadingSave">保存</el-button>
        </div>
        <!-------------------------------按钮 ----------------------up ------------------->
        <el-card style="height: 630px">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="项目名称:">
              <el-tag size="medium" @click="open = true">{{ selPlan.name }}</el-tag>
            </el-form-item>
            <el-form-item label="指标名称:" prop="name">
              <el-input v-model="form.name" />
            </el-form-item>
            <el-form-item label="指标描述:" prop="describe">
              <el-input v-model="form.describe" />
            </el-form-item>
          </el-form>
        </el-card>
      </el-card>
      <!-------------------------------form表单----------------------up ------------------->
    </div>

    <el-dialog :visible.sync="open" width="60%" append-to-body>
      <el-row :gutter="20" style="margin: 10px">
        <el-col :span="6">
          <el-input v-model="planQuery.name" placeholder="项目名称" />
        </el-col>
        <el-col :span="6">
          <el-button class="hand-btn" type="primary" icon="el-icon-search" @click="planSearch">查询</el-button>
        </el-col>
      </el-row>
      <el-table
        :data="planPage.content"
        tooltip-effect="dark"
        v-loading="loadingPlanTable"
        border
        @row-click="selTable"
      >
        <el-table-column
          type="index"
          label="项次"
          width="50"
        />
        <el-table-column
          prop="name"
          label="项目名称"
        />
        <el-table-column
          prop="specs"
          label="项目描述"
        />
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,prev, pager, next"
          :current-page="planQuery.pageIndex"
          :page-size="planQuery.pageSize"
          :total="planPage.totalElements"
        />
      </div>
    </el-dialog>
  </div>
</template>
<script>import { save } from '@/api/baseQuota'
import { query } from '@/api/baseQuota'
import { query as queryplan } from '@/api/basePlan'
import { delbyid } from '@/api/baseQuota'
export default {
  name: 'Basequota',
  data() {
    return {
      rules: {
        name: [
          { required: true, message: '指标不能为空' }
        ],
        projectname: [
          { required: true, message: '项目不能为空' }
        ]
      },
      open: false,
      selPlan: {},
      editVisible: false,
      query: {
        pageIndex: 1,
        pageSize: 20,
        name: ''
      },
      planQuery: {
        pageIndex: 1,
        pageSize: 10,
        name: ''
      },
      page: {},
      planPage: {},
      form: {},
      loadingSave: false,
      loadingMTable: false,
      loadingPlanTable: false
    }
  },
  created() {
    this.getData()
    this.planSearch()
  },
  methods: {
    // 获取 easy-mock 的模拟数据
    getData() {
      this.loadingMTable = true
      query(this.query).then(res => {
        this.loadingMTable = false
        this.page = res.data.page
      })
    },
    getPlan() {
      this.loadingPlanTable = true
      queryplan(this.planQuery).then(res => {
        this.loadingPlanTable = false
        this.planPage = res.data.page
      })
    },
    selTable(row, column, cell, event) {
      this.selPlan = row
      this.open = false
    },
    // 触发搜索按钮
    planSearch() {
      this.$set(this.planQuery, 'pageIndex', 1)
      this.getPlan()
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, 'pageIndex', 1)
      this.getData()
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm('确定要删除吗？', '提示', {
        type: 'warning'
      }).then(() => {
        delbyid(row.id).then(res => {
          this.$message.success('删除成功')
          this.getData()
        })
      })
        .catch(() => {
        })
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    delAllSelection() {
      const length = this.multipleSelection.length
      let str = ''
      this.delList = this.delList.concat(this.multipleSelection)
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' '
      }
      this.$message.error(`删除了${str}`)
      this.multipleSelection = []
    },
    // 编辑操作
    handleEdit(index, row) {
      this.selPlan = row.basePlan
      this.form = row
      this.editVisible = true
    },
    // 保存编辑
    saveEdit() {
      this.$refs.form.validate((valid) => {
        if (this.selPlan.id) {
          if (valid) {
            this.form.refId = this.selPlan.id
            this.loadingSave = true
            save(this.form).then(res => {
              this.loadingSave = false
              this.$message.success(res.message)
              this.editVisible = false
              this.getData()
            })
          } else {
            this.$message.warning('信息未填写完整')
          }
        } else {
          this.$message.warning('请选择项目!')
        }
      })
    },
    add() {
      this.form = {}
      this.selPlan = {}
      this.editVisible = true
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, 'pageIndex', val)
      this.getData()
    },
    // 分页大小改变
    handleSizeChange(val) {
      this.$set(this.query, 'pageSize', val)
      this.getData()
    }
  }
}
</script>

<style scoped>
  .handle-box {
    margin: 20px;
  }

  .handle-select {
    width: 120px;
  }
  .handle-input {
    margin: 10px;
    width: 300px;
    display: inline-block;
  }
  .table {
    width: 100%;
    font-size: 14px;
  }
  .red {
    color: #ff0000;
  }
  .hand-btn {
    margin: 5px;
  }
  .pagination{
    margin-left: 50px;
  }
</style>
